<?php

class StaticmgrController extends Controller {

	public function index() {
		$this->getSmarty ();
		$this->smarty->display ( "admin/static/static.tpl" );
	}
	
	public function indexcreate(){
		$this->getSmarty();
        require_once SERVICE . DS . 'IndexService.class.php';
        $indexService = new IndexService( $this->getDB () );
        $result = $indexService->loadPosts4ListCategory(0,30);
        $this->smarty->assign ( 'posts_html', $result );

        require_once SERVICE . DS . 'ad' . DS . 'AdService.class.php';
        $service = new AdService ( $this->getDB () );
        
        $curCategory_name = $service->getImgshowHtml();
        $this->smarty->assign ( 'curCategory_name', $curCategory_name );
        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'tags',Constant::getHotTag() );
        $this->smarty->assign ( 'pagesets', Constant::indexHeader() );


		$content = $this->smarty->fetch("index.tpl");
		$file_name = PROJECT."/index.html";
		$fp = fopen($file_name, "w");
		fwrite($fp, $content);
		fclose($fp);
		
		$result = array(
			success=>true,
			file=>$file_name
		);
		
		echo json_encode ($result );
	}
    public function navcreate(){
        $this->getSmarty();
        //关于cache的解决方案
        require_once SERVICE . DS . 'admin' . DS . 'PostCategoryService.class.php';
        $category = new PostCategoryService($this->getDB());
        $pcategrys = $category->getCategoryName(" parent = 1 ");

        foreach($pcategrys as $pcategry){
            $cachearray [] = array("parent"=>$pcategry, "childres"=>$category->getCategoryName(" parent = ".$pcategry->category_id));
        }
       // echo $arrstr;
        $strs = serialize($cachearray);
        $file_name = PROJECT."/model/cache/nav";
        $fp = fopen($file_name, "w");
        fwrite($fp, $strs);
        fclose($fp);
        $result = array(
            success=>true,
            file=>$file_name
        );
        echo json_encode ($result );
    }
    public function hottagcreate(){
        $this->getSmarty();
        //关于cache的解决方案
         require_once SERVICE . DS . 'admin/CommonService.class.php';
         $common = new CommonService($this->getDB());
         $hostsql = "select p.tag_id id,t.tag_name name ,count(p.tag_id) countnm from  tags t,post_tag p where t.tag_id = p.tag_id GROUP BY  p.tag_id   order by p.create_date,countnm desc limit 15";
        $tagarray = $common->query($hostsql);
        // echo $arrstr;
        $strs = serialize($tagarray);
        $file_name = PROJECT."/model/cache/hottag";
        $fp = fopen($file_name, "w");
        fwrite($fp, $strs);
        fclose($fp);


        $hostcategory = "select t.category_id id,t.category_name name ,count(t.category_id) countnm from  post_category t,posts p where t.category_id = p.post_category and  t.category_id != 1 GROUP BY  p.post_category   order by countnm desc limit 10";
        $hostcatearray = $common->query($hostcategory);
        // echo $arrstr;
        $strs = serialize($hostcatearray);
        $file_name = PROJECT."/model/cache/hotcate";
        $fp = fopen($file_name, "w");
        fwrite($fp, $strs);
        fclose($fp);

        $result = array(
            success=>true,
            file=>$file_name
        );
       echo json_encode ($result );
}

    
    public function categorycreate(){
    	
    	$this->getSmarty();
    	//关于cache的解决方案
    	require_once SERVICE . DS . 'admin' . DS . 'PostCategoryService.class.php';
    	$category = new PostCategoryService($this->getDB());
    	require_once SERVICE . DS . 'CategoryService.class.php';
		$categoryService = new CategoryService( $this->getDB () );
    	$pcategrys = $category->getCategoryName(" parent = 1 ");
    	
    	foreach($pcategrys as $pcategry){
    		$cat_id = $pcategry->category_id;
    		if(!file_exists(PROJECT."/".$cat_id)){
    			mkdir(PROJECT."/".$cat_id);
    		}
//    		$where = " AND category_id = ".$cat_id;
			$where = "";
	    	if (empty($cat_id)){
				$where = "";
			}else {
				//判断分类节点
				$result = $categoryService->getCategoryId($cat_id);
				$where = " category_id in ( ".rtrim($result, ",").") AND ";
			}
    		$this->getTemplatePosts($orderBy=" order by  post_date desc ",$where);
    		require_once COMMON . DS  . 'Constant.class.php';
	        $this->smarty->assign ( 'tags',Constant::getHotTag() );
            $this->smarty->assign ( 'pagesets', Constant::categoryHeader($pcategry) );
            $this->smarty->assign ( 'categoryname', Constant::getCategoryName($cat_id) );
            $this->smarty->assign ( 'cateogoryid', $cat_id );
            $this->smarty->assign ( 'crumb', Constant::getSecCategoryNav($cat_id) );
            $content = $this->smarty->fetch("category.tpl");
    		$file_name = PROJECT."/".$cat_id."/index.html";
    		$fp = fopen($file_name, "w");
    		fwrite($fp, $content);
    		fclose($fp);
    		
    		//
    		$categrys = $category->getCategoryName(" parent = ".$pcategry->category_id);
    		foreach($categrys as $cat){
    			$sub_cat_id = $cat->category_id;
    			$where = " category_id = ".$sub_cat_id." AND ";
    			$this->getTemplatePosts($orderBy=" order by  post_date desc ",$where);
    			require_once COMMON . DS  . 'Constant.class.php';
    			if(!file_exists(PROJECT."/".$cat_id."/".$sub_cat_id)){
    				mkdir(PROJECT."/".$cat_id."/".$sub_cat_id);
    			}
    			require_once COMMON . DS  . 'Constant.class.php';
		        $this->smarty->assign ( 'tags',Constant::getHotTag() );
                $this->smarty->assign ( 'pagesets', Constant::categoryHeader($cat) );
                $this->smarty->assign ( 'categoryname', Constant::getCategoryName($sub_cat_id) );
                $this->smarty->assign ( 'cateogoryid', $sub_cat_id );
                $this->smarty->assign ( 'crumb', Constant::getSecCategoryNav($sub_cat_id) );
                $content = $this->smarty->fetch("category.tpl");
    			$file_name = PROJECT."/".$cat_id."/".$sub_cat_id."/index.html";
    			$fp = fopen($file_name, "w");
    			fwrite($fp, $content);
    			fclose($fp);
    		}
    	}
    	
    	$result = array(
    			success=>true,
    			file=>$file_name
    	);
    	
    	echo json_encode ($result );
    }

    
    
    /**
     * 在这里生成文章静态页。传入一个postid
     * 然后替换post.tpl，将内容写成文件。
     * 文件的路径为:
     * 项目根目录/父分类id/子分类id/文章id.html
     * 如果文章的分类是一级的路经为
     * 项目根目录/分类id/文章id.html
     */
    public function postcreate(){
    	$this->getSmarty();
    	
    	require_once SERVICE . DS . 'admin' . DS . 'PostCategoryService.class.php';
    	require_once SERVICE . DS . 'admin' . DS . 'PostsService.class.php';
    	
    	$category = new PostCategoryService($this->getDB());
    	$relation = $category->getCategoryRealation();
    	//统一生产目录
    	$category->mkCatDir();
    	
    	$post = new PostsService($this->getDB());
    	$posts = $post->getAllPosts();
    	
    	foreach($posts as $post){
    		$param = array("id"=>$post->ID);
    		$this->postcreatebyid($param,$relation);
    	}
    	
    	$result = array(
    		success=>true,
    	);
    	
    	echo json_encode ($result );
    }
    
    public function postcreatebyid($param,$relation=null){
    	
    	$postid = $param['id'];

    	$sigle = false;;
    	
    	require_once SERVICE . DS . 'admin' . DS . 'PostCategoryService.class.php';
    	require_once SERVICE . DS . 'PostService.class.php';
    	$service = new PostService ( $this->getDB () );
    	$category = new PostCategoryService($this->getDB());
    	
    	if($relation == null){
    		$sigle = true;
    		$relation = $category->getCategoryRealation();
    	}
    	$postelement = $service->getPost ( $postid );
    	$postComents = $service->getComments ( $postid );
    	
    	$data = array ();
    	foreach ( $postComents as $re ) {
    		if (empty ( $re->user_photo )) {
    			$photo = "public/photo/user-iocn.gif";
    		} else {
    			$photo = $re->user_photo;
    		}
    	
    		$data [] = array (
    				"user_photo" => $photo,
    				"username" => $re->username,
    				"comment_ID" => $re->comment_ID,
    				"comment_date" => $re->comment_date,
    				"comment_content" => $re->comment_content
    		);
    	}
    	$post = $postelement [0];
        //设置上一篇内容
        $propost = $service->getPrePost ( $postid );
        //设置下一篇内容
        $nxtpost = $service->getNxtPost( $postid );
        $preAndnxt = array($propost[0],$nxtpost[0]);
        require_once SERVICE . DS . 'IndexService.class.php';
        $indexService = new IndexService( $this->getDB () );
        $indexService->fillPostHref($preAndnxt);
        $propost = $preAndnxt[0];
        $nxtpost =  $preAndnxt[1];
        if(!empty($propost)){

            //设置上一篇内容
            $pre_post =  array (
                "url" =>  $propost->href,
                "name" => $propost->post_title
            );
        }else{
            $pre_post =  array (
                "url" =>  "",
                "name" => "这是第一篇"
            );
        }
        if(!empty($nxtpost)){

            //设置下一篇内容
            $nxt_post =  array (
                "url" =>$nxtpost->href,
                "name" => $nxtpost->post_title
            );
        }else{
            $nxt_post =  array (
                "url" => "",
                "name" => "已经是最后一篇了"
            );
        }


        $tagarr = $service->getTagIdByName($postid);
    	if (! empty ( $tagarr )) {
    		foreach ( $tagarr as $tag ) {
    			$tagdata [] = array (
    					"url" => WEBSITE_URL."tag/showtag/".$tag->tag_id,
    					"name" => $tag->tag_name
    			);
    		}
    	}
    	if($this->smarty == null){
    		$this->getSmarty();
    	}
    	
    	$this->smarty->assign ( 'postComents', $data );
    	$this->smarty->assign ( 'postelement', $post );
    	$this->smarty->assign ( 'posttags', $tagdata );
    	$this->smarty->assign ( 'post', $post );
    	//热门标签
    	require_once COMMON . DS  . 'Constant.class.php';
    	$this->smarty->assign ( 'tags',Constant::getHotTag() );
    	$this->smarty->assign ( 'crumb', Constant::getSecCategoryNav($post->post_category) );
    	$this->smarty->assign ( 'pagesets',Constant::postHeader($postid) );
    	$this->smarty->assign ( 'pre_post', $pre_post );
    	$this->smarty->assign ( 'nxt_post', $nxt_post );
    	$this->smarty->assign ( 'postid', $postid );
    	
    	$catPath = PROJECT;
    	$pCatId = $relation[$post->post_category];
    	
    	if($pCatId != null && $pCatId != "" && "0" != $pCatId && "1" != $pCatId){
    		$catPath = $catPath."/".$pCatId;
    		if(!file_exists($catPath)){
    			mkdir($catPath);
    		}
    	}
    	
    	if($post->post_category != null && $post->post_category != ""){
	    	$catPath = $catPath."/".$post->post_category;
	    	if(!file_exists($catPath)){
	    		mkdir($catPath);
	    	}
    	}
    	
    	$content = $this->smarty->fetch("post.tpl");
    	$file_name = $catPath."/$postid.html";
    	$fp = fopen($file_name, "w");
    	fwrite($fp, $content);
    	fclose($fp);
    	
    	if($sigle){
    		$result = array(
    				success=>true,
    		);
    		 
    		echo json_encode ($result );
    	}
    }
    /**
     *
     * 获取文章前10条数据
     */


    public function getTemplatePosts($orderBy, $where){
       require_once SERVICE . DS . 'IndexService.class.php';

		$indexService = new IndexService( $this->getDB () );
		
        $result = $indexService->loadPosts4ListCategory(0,30,$orderBy, $where);
		
		$this->smarty->assign ( 'posts_html', $result );
    }
    public function headerDatas(){
        require_once COMMON . DS  . 'Constant.class.php';
//        $js=array(WEBSITE_URL."public/js/jquery.imagesloaded.js",
//            WEBSITE_URL."public/js/jquery.wookmark.min.js",
//            WEBSITE_URL."public/js/listBox.js",
//        WEBSITE_URL."public/js/index.js");
        $pagesets = array("seo"=>Constant::$seoarray,"nav"=>Constant::getNav());

        return $pagesets;
    }
}

?>